/* 
    <provides OS-like functionality using a time-triggered scheduler>
    Copyright (C) <2012>  <Jonathan D. Armstrong> <jonathan.armstrong AT mavs.uta.edu>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file   nortos.h
 * @Author Jonathan D. Armstrong (jonathan.armstrong@mavs.uta.edu)
 * @date   December, 2012
 * @brief  Provides access to nortos functionality.
 *
 * Include file.
 */
 
#ifndef NORTOS_H
#define NORTOS_H

/* allow use of 'uint8_t', etc. */
#include <stdint.h>

/* max number of tasks available in the task control block (default: 10);
   more task capacity creates more RAM usage */
#define MAX_TASKS 10

/* available task states */
typedef enum task_state_t
{
    INVALID = 0,
    BLOCKED,
    DELAYED,
    READY,
}task_state_t;

/* 
    task control block
    - just a useful structure to store information
      concerning individual tasks
*/
/* FIXME: need 'priority' field? */
/* FIXME: functions pointed to by function pointers to accept, return arguments even
          if not used by every task? */
struct
{
    task_state_t state;         // state of task (see enum above)
    uint8_t (*functionPtr)(uint8_t);  // pointer to function containing time-triggered task
    uint32_t pid;               // unique process ID related to function address
    uint8_t  priority;          // unused currently
    uint32_t period;            // total time in ticks between task runs
    uint32_t delay;             // current time in ticks until next task run
} tasks[MAX_TASKS];

/* function prototypes */
void init_tasks(void);
int create_task(uint8_t (*functionPtr)(uint8_t), uint32_t priority, enum task_state_t state);
int delete_task(uint8_t (*functionPtr)(uint8_t));
int block_task(uint8_t (*functionPtr)(uint8_t));
int unblock_task(uint8_t (*functionPtr)(uint8_t));
int8_t scheduler(void);
void nortos_run(void);

#endif // NORTOS_H